package com.finance.controller.user.personal;

import com.finance.common.Result;
import com.finance.pojo.others.Loan;
import com.finance.pojo.user.User;
import com.finance.service.user.personal.MyLoanService;
import com.finance.service.user.personal.SecurityService;
import com.finance.service.user.userinfo.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoanAndSecurityController {
    @Autowired
    private MyLoanService myLoanService;
    @Autowired
    private SecurityService securityService;
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 用户还款，id为指定的未还清的贷款记录
     * @param id
     * @return
     */
    @RequestMapping("/user/repayment/{id}")
    @ResponseBody
    public Result updateMyLoanRecords(@PathVariable("id") Integer id){
        Loan loan = new Loan();
        loan.setId(id);
        loan.setLoanstatus(2);
        int i = myLoanService.updateMyLoan(loan);
        if(i == 1){
            return Result.success();
        }else{
            return Result.fail();
        }

    }

    /**
     * 修改用户个人账户的密码
     * @param userId
     * @param oldpwd
     * @param newpwd
     * @param session
     * @return
     */
    @RequestMapping("/user/updatePwd")
    @ResponseBody
    public Result updatePwd(@RequestParam("id") Integer userId,
                            @RequestParam("oldpwd") String oldpwd,
                            @RequestParam("newpwd") String newpwd,
                            HttpSession session){

        User user = (User)session.getAttribute("loginUser");
        if (user==null) return  Result.fail();
        if (oldpwd.equals(user.getPassword())){
            user.setPassword(newpwd);
            int i = securityService.updateUser(user);
            if( i == 1){
                //System.out.println("密码修改成功");
                session.setAttribute("loginUser",user);
                return Result.success();
            }else{
                //System.out.println("密码修改失败");
                return Result.fail();
            }
        }else
            return Result.fail();

    }

    /**
     * 用户注销自己的账户
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("/user/destroyAccount")
    @ResponseBody
    public Result destroyAccount(@RequestParam("password") String password, HttpSession session){

        User user = (User)session.getAttribute("loginUser");//从当前session中获取用户信息
        if(password.equals(user.getPassword())){
            int i = userInfoService.deleteUserById(user.getId());
            if(i==1){            //关闭session并退出在html自动跳转退出登陆
                //System.out.println("注销账户成功");
                return Result.success();
            }else {
                //System.out.println("注销账户失败");
                return Result.fail();
            }
        }else
            return Result.fail();
    }

}
